package com.amigo.online.consumer.video.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.consumer.video.exception.FileUploadException;

/**
 *  <code>UploadImgUtil.java</code>
 *  <p>功能:图片上传公共类
 *  
 *  <p>Copyright 南京阿觅谷 2018 All right reserved.
 *  @author LILONG 时间 2018年6月21日 下午3:26:03	
 *  @version 1.0 
 *  </br>最后修改人 无
 */
@Component
@PropertySource(value= "classpath:bootstrap.yml")
public class UploadImgUtil {
	
	@Autowired
	FastDFSClient fastDFSClient;
	
	private static Logger logger = Logger.getLogger(UploadImgUtil.class);
	
	/**
	 * 功能:
	 * <p>作者 LILONG 2018年6月21日 下午3:25:54
	 * @param mulfile
	 * @param associateId
	 * @return
	 */
	public static List<String> uploadImg(MultipartFile[] mulfile) {
		List<String> fileIdList = new ArrayList<String>();
		if (mulfile != null && mulfile.length > 0) {
			try {
				for (int i = 0; i < mulfile.length; i++) {
					if (!mulfile[i].isEmpty()) {
						String fileid = FastDFSClient.uploadFile(mulfile[i].getBytes(), null);
						if(StringUtils.isNotEmpty(fileid)) {
							fileIdList.add(fileid);							
						}else {
							throw new FileUploadException(StatusCode.FILE_UPLOAD_FILURE);
						}
					}else {
						throw new FileUploadException(StatusCode.NO_FILE_ACQUIRE);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new FileUploadException(StatusCode.FILE_UPLOAD_FILURE);
			}
		}
		return fileIdList;
	}
	
	/**
	 * 上传文件
	 * @param inputStream
	 * @return
	 */
	public String uploadSingleImgByInputStream(InputStream inputStream) {
		String result = null;
		try {
			byte[] fileBuff = null;
			if (inputStream != null) {
				//输入流中可读取字节数
				int len = inputStream.available();
				fileBuff = new byte[len];
				inputStream.read(fileBuff);
			}
			result = fastDFSClient.uploadFile(fileBuff, null);
		} catch (IOException e) {
			logger.error("文件上传失败:" + e);
		} catch (Exception e) {
			logger.error("文件上传失败:" + e);
		}
		return result;
	}
	
	public static String uploadSingleFile(MultipartFile file) {
		String result = null;
		if (!file.isEmpty()) {
			try {
				result = FastDFSClient.uploadFile(file.getBytes(), null);
			} catch (Exception e) {
				logger.error("获取文件字节数据失败失败:{}!", e); 
			}
			if (StringUtils.isEmpty(result)) {
				logger.error("文件上传返回的地址为空!");
				throw new FileUploadException(StatusCode.FILE_UPLOAD_FILURE);
			}
		}		
		return result;
	}
	
	public static String concurrentUploadSingleFile(MultipartFile file) {
		String result = null;
		if (!file.isEmpty()) {
			try {
				result = FastDFSClient.concurrentUploadFile(file.getBytes(), null);
			} catch (Exception e) {
				logger.error("获取文件字节数据失败失败:{}!", e); 
			}
			if (StringUtils.isEmpty(result)) {
				logger.error("文件上传返回的地址为空!");
				throw new FileUploadException(StatusCode.FILE_UPLOAD_FILURE);
			}
		}		
		return result;
	}
	
	
	/**
	 * 功能:删除临时文件
	 * <p>
	 * 作者 LILONG 2018年6月4日 下午2:49:39
	 * 
	 * @param file
	 */
	private void deleteTempFile(File file) {
		if (file.exists()) {
			file.delete();
		}
	} 

	/**
	 * 功能:
	 * <p>
	 * 作者 LILONG 2018年6月4日 下午4:08:39
	 * 
	 * @param ins
	 * @param file
	 */
	public void inputStreamToFile(InputStream ins, File file) {
		try {
			OutputStream os = new FileOutputStream(file);
			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
			os.close();
			ins.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public int deleteFile(String fileId) {
		return 0;//FastDFSClient.deleteFile(fileId);
	}
}